/*************************************************************************
    > File Name: face_serialize.h
    > Author: fanzhihui
    > Mail: 953106194@qq.com 
    > Created Time: 2019年06月26日 星期三 15时58分11秒
 ************************************************************************/
#ifndef _FHTTPD_FACE_SERIALIZE_H_
#define _FHTTPD_FACE_SERIALIZE_H_
#include<iostream>
#include <fstream>
#include <string>
#include <vector>
#include <exception>
#include <sstream> 
#include "json/json.h"
#include "face_info.h"

using namespace std;

#define SERIAL_ADD_FACE 1
#define SERIAL_DELETE_FACE 2

typedef struct face_serial_info_
{
	kown_face_info_t face;
	int op_flag;
	void UnSerialize(const string& line)
	{
		//std::cout << "UnSerialize"<< std::endl;
		Json::Reader reader;
		Json::Value json_object;

		reader.parse(line.c_str(), json_object);
		op_flag = json_object["op_flag"].asInt();
		face.face_id = json_object["face_id"].asString();
		face.face_key = json_object["face_key"].asString();

        //std::cout << "========" << op_flag << " " << "face id " << face.face_id <<" face key:" << face.face_key << std::endl; 
		
		for( uint32_t i = 0; i < 512; i++){
			string key = "fe"+to_string(i);
			face.features.push_back(json_object[key].asDouble());

		}

	}


	string Serialize() const
	{

		Json::Value  valueinfo;

		valueinfo["op_flag"] = op_flag;   
		valueinfo["face_id"] = face.face_id;
		valueinfo["face_key"] = face.face_key;

		//Json::Value valuefeatures;
		for(uint32_t i =0; i < face.features.size(); ++i) {
			//valuefeatures.append(face.features[i]);
			string key = "fe" + to_string(i);
			valueinfo[key] = face.features[i];
		}
		//valueinfo["features:"] = valuefeatures;

		Json::FastWriter writerinfo;
		return writerinfo.write(valueinfo);
	}

}face_serial_info_t;


class FaceSerializeIf
{
public:
	FaceSerializeIf(){

	}
	virtual ~FaceSerializeIf()
	{
	}

	virtual void Serialize(const face_serial_info_t & face) = 0;
	virtual void UnSerialize(vector<face_serial_info_t>& faces) = 0;
	virtual void Clear() = 0;
};

class FaceSerializeException : public std::exception
{
public:
	FaceSerializeException(const std::string& message):
		msg(message)
	{
	}

	const char* what() const noexcept override {
		return msg.c_str();
	}
private:

	std::string msg;
};


class FileFaceSerialize : public FaceSerializeIf
{
public:
	FileFaceSerialize(const string& file);
	~FileFaceSerialize();

	virtual void Serialize(const face_serial_info_t& face);
	virtual void UnSerialize(vector<face_serial_info_t>& faces);
	virtual void Clear();
private:

	string filename;

	ifstream fIn;
	ofstream fOut;

};

#endif

